package com.project.common.core.utils.thumbnailator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PictureUtils {


    /**
     * 切成圆形
     * @param pathName   图片路径
     * @throws IOException
     */
    public static void tailor(String pathName) throws IOException {

        BufferedImage bi1 = ImageIO.read(new File(pathName));

        // 根据需要是否使用 BufferedImage.TYPE_INT_ARGB
        BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
                .getHeight());

        Graphics2D g2 = image.createGraphics();
        image = g2.getDeviceConfiguration().createCompatibleImage(bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT);
        g2 = image.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(image.getWidth(), image.getHeight()));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2 = image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRoundRect(0, 0, bi1.getWidth(), bi1.getHeight(), image.getWidth(), image.getHeight());
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(bi1, 0, 0, bi1.getWidth(), bi1.getHeight(), null);
        g2.dispose();

        try {
            ImageIO.write(image, "png", new File(pathName));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (image!=null){
                image.flush();
                image =null;
            }
            if (g2!=null){
                g2 =null;
            }
        }
    }

    /**
     * 切圆角
     * @param srcImageFile  原图
     * @param result        终图
     * @param type          格式
     * @param cornerRadius  圆滑度
     * @throws Exception
     */
    public static void makeRoundedCorner(File srcImageFile, File result, String type, int cornerRadius) throws Exception {
        BufferedImage bi1 = ImageIO.read(srcImageFile);

        // 根据需要是否使用 BufferedImage.TYPE_INT_ARGB
        BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
                .getHeight());

        Graphics2D g2 = image.createGraphics();
        image = g2.getDeviceConfiguration().createCompatibleImage(bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT);
        g2 = image.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(image.getWidth(), image.getHeight()));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2 = image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRoundRect(0, 0,bi1.getWidth(), bi1.getHeight(), cornerRadius, cornerRadius);
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(bi1, 0, 0, bi1.getWidth(), bi1.getHeight(), null);
        g2.dispose();
        ImageIO.write(image, type, result);
    }


    /*public static void main(String[] args) throws Exception {
        String path="C:/Users/JoJo/Desktop/img/";
        String path1=path + "201811151049473770001.png";
        String path2=path + StringUtils.getMajorKey() + ".png";
        *//*File file1 = new File(path + "pic1.png");
        File file2 = new File(path + StringUtils.getMajorKey() + ".png");
        makeRoundedCorner(file1,file2,"png", 150);*//*
        //ThumbnailatorUtils.ImgSourceRegion(path1,path2, Positions.CENTER,768,768,600,600,true);

        BufferedImage image = ImageIO.read(new File(path1));
        int width = image.getWidth();
        int height = image.getHeight();

        ThumbnailatorUtils.ImgSourceRegion(path1,path2, 0,0,width,height-100,width-300,height-100,true);
    }*/

}
